﻿<%@ WebHandler Language="C#" Class="selector" %>
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Text;

/// <summary>
/// 获取数据字典内容项专用后台
/// </summary>
public class selector : AjaxHandlerBase {

    public override void RequestProcess(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string strType = context.Request["type"];
        string strKey = context.Request["key"];
        string strSubkey = context.Request["subkey"];

        Guid companyId = CurrentUserInfo.CompanyId;
        //添加人：熊世宏
        //添加日期：2010-05-26
        //描述：接受公司id参数，默认是当前用户的公司id
        Guid cityId = CurrentUserInfo.CityId;
        string filter = "bycompany";
        
        //添加人：熊世宏
        //添加日期：2010-05-19
        //描述：接受公司id参数，默认是当前用户的公司id
        if (context.Request["companyid"] != null)
        {
            companyId = new Guid(context.Request["companyid"]);
        }
        if (context.Request["filter"] != null)
        {
            filter = context.Request["filter"];
        }
        if (strKey == "System.Block" || strKey == "System.Region")
        {
            filter = "bycity";
            if (context.Request["recordid"] != null)
            {
                Chenxin.Lib.House.Entity.DataDictionary model = Chenxin.Lib.House.DAL.DataDictionary.GetModel(new Guid(context.Request["recordid"]));
                cityId = model.CityId;
            }
        }

        //添加人：熊世宏
        //添加日期：2010-05-26
        //描述：接受公司id参数，默认是当前用户的公司id
        if (context.Request["CityId"] != null)
        {
            cityId = new Guid(context.Request["cityid"]);
        }
        
        //Guid companyId = new Guid("bda987bb-3d9d-4fcc-96ac-8cd9e5df7bd0");
        string strResponse = "";
        switch (strType.ToLower())
        {
            case "datadic":
                if (filter == "bycompany")
                {
                    strResponse = GetDataDicByKey(companyId, strKey, strSubkey, filter);
                }
                else//熊世宏 2010-05-26
                {
                    strResponse = GetDataDicByKey(cityId, strKey, strSubkey, filter);                    
                }
                break;
            default:
                break;
        }

        context.Response.Write(strResponse);      
    }
    
    /// <summary>
    /// 根据Key获取数据字典中相应的字典项目
    /// </summary>
    /// <param name="strKey"></param>
    /// <returns></returns>
    private string GetDataDicByKey(Guid objectid, string strKey, string strSubkey,string filter)
    {
        try
        {
            System.Collections.Generic.List<Chenxin.Lib.House.Entity.DataDictionary> dicList = null;//new System.Collections.Generic.List<Chenxin.Lib.House.Entity.DataDictionary>();

            //首先从缓存中获取数据
            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.DICTDATA);
            string strCacheKey = string.Format(Chenxin.Lib.Cache.CacheDataType.DICTDATA, strKey + strSubkey + "." + objectid.ToString());
            dicList = cs.GetList<Chenxin.Lib.House.Entity.DataDictionary>(strCacheKey);

            if (!cs.Hit)//若缓存未命中，则从数据库中获取并存入缓存，时间为1天
            {
                string strWhere = "";
                if (filter == "bycompany")
                {
                    strWhere = string.Format("CompanyId = '{0}' AND ItemKey = '{1}'", objectid, strKey);
                }
                else
                {
                    strWhere = string.Format("CityId = '{0}' AND ItemKey = '{1}'", objectid, strKey);                 
                }


                if (strKey == "System.Block" && !string.IsNullOrEmpty(strSubkey))
                {
                    strWhere = strWhere + string.Format(" AND (substring(Convert(varchar(50), itemIndex), 1 ,len(itemIndex)-3)) ='{0}'", strSubkey);
                }

                dicList = Chenxin.Lib.House.DAL.DataDictionary.GetListArray(strWhere, "ItemIndex");

                if (dicList != null && dicList.Count>0) cs.Set(strCacheKey, dicList, DateTime.Today.AddDays(1));
            }

            //生成JSON字符串
            System.Text.StringBuilder str = new System.Text.StringBuilder();
            foreach (Chenxin.Lib.House.Entity.DataDictionary item in dicList)
            {
                str.Append("{ txt: '" + item.ItemValue.Trim() + "', val: '" + item.ItemIndex + "',id:'" + item.RecordId + "' },");
            }
            if (str.Length > 0) str.Remove(str.Length - 1, 1);
            return string.Format("{{success:'1',msg:'成功',data:{{items:[{0}]}}}}", str.ToString());
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'意外错误：{0}',data:{{}}}}",ex.Message);
        }
    }

 }